package customControls;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jaden.washeradministrator.*;

import java.util.List;

import order.Order;

public class OrderConfirmedAdapter extends ArrayAdapter<Order>implements View.OnClickListener {

    Order order;
    int Position;
    private int resourceId;
    private static final String TAG = "OrderConfirmedAdapter";
    public OrderConfirmedAdapter(@NonNull Context context, int resource, List<Order> objects) {
        super(context, resource, objects);
        resourceId = resource;
    }
    public class ViewHolder
    {
        TextView orderID;
        TextView washerID;
        TextView price;
        TextView number;
        TextView orderstatus;
        Button detailinfo;
    }
    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        Position=position;
        //根据position 得到当前子项的Commodity实例
        order=getItem(position);
        ViewHolder viewHolder = null;
        if(convertView==null)
        {
            Log.d(TAG, "getView: "+position);
            //为当前子项加载指定布局->resourceId
            convertView=LayoutInflater.from(getContext()).inflate(resourceId,parent,false);
            viewHolder=new ViewHolder();
            //获取控件实例
            getInstance(viewHolder,convertView);
            convertView.setTag(viewHolder);
        }
        else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        setClickListener(viewHolder);
        //显示控件信息
        showInformation(viewHolder);
        return convertView;
    }

    //获取控件实例
    private void getInstance(ViewHolder viewHolder, View view) {
        viewHolder.orderID=(TextView)view.findViewById(R.id.textView_orderID_orderlistviewitemcomfirmed);
        viewHolder.washerID=(TextView)view.findViewById(R.id.textView_washerID_orderlistviewitemcomfirmed);
        viewHolder. price=(TextView)view.findViewById(R.id.textView_price_orderlistviewitemcomfirmed);
        viewHolder.number=(TextView)view.findViewById(R.id.textView_number_orderlistviewitemcomfirmed);
        viewHolder.orderstatus=(TextView)view.findViewById(R.id.textView_orderstatus_orderlistviewitemcomfirmed);
        viewHolder.detailinfo=(Button)view.findViewById(R.id.button_detailinfo_orderlistviewitemcomfirmed);
    }
    //显示控件信息
    private void showInformation(ViewHolder viewHolder) {
        viewHolder.orderID.setText(viewHolder.orderID.getText()+String.valueOf(order.getOrderID()));
        viewHolder.washerID.setText(viewHolder.washerID.getText()+String.valueOf(order.getWasherID()));
        viewHolder.price.setText(viewHolder.price.getText()+String.valueOf(order.getOrderMoney()));
        viewHolder.number.setText(viewHolder.number.getText()+String.valueOf(Position));
        if(order.getOrderStatus()==4)
            viewHolder.orderstatus.setText("已完成");
        else
            viewHolder.orderstatus.setText("正在洗衣");
    }
    private void setClickListener(ViewHolder viewHolder) {
        viewHolder.detailinfo.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_detailinfo_orderlistviewitemcomfirmed:
               /* Intent intent=new Intent(getContext(), OrderDetailInformationPage.class);
                intent.putExtra("position",Position);
                getContext().startActivity(intent);
                */
                Toast.makeText(getContext(),"查看订单详情",Toast.LENGTH_LONG).show();
                break;
        }
    }
}
